<?php
namespace V5;
class C_History extends \V4\C_History{

    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require user uid';
        }
        if (!isset($req['userType'])) {
            return 'require user type';
        }
        if (!isset($req['k']) || !isset($req['t'])) {
            return 'data error';
        }
        if (!isset($req['enterpriseId'])) {
            return 'require enterprise id';
        }
        if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'], $req['enterpriseId'], $req['t'], $req['k']) == false) {
            return 'data illegal';
        }
        if (!isset($req['channelId'])) {
            return 'require channel id';
        }

        $result = array('product_list' => array());
        $customer_info = \Biz\EnterpriseCustomerHandler::getEnterpriseCustomerById($req['enterpriseId'], $req['uid']);
        $customer_info = self::ormFormatCustomerInfo($customer_info);
        if ($customer_info['channel_id'] != $req['channelId']) {    //验证用户渠道
            return 'invalid channel id ';
        }
        if ($customer_info['enterprise_id'] != $req['enterpriseId']) {//验证用户企业
            return 'invalid enterprise id ';
        }
        $channel_id = $req['channelId'];
        $enterprise_id = $req['enterpriseId'];
        $talabtion = new \Cls\Tabulation();

        $talabtion->setWhere('enterprise_customer_id', $req['uid'])
            ->setOrderBy('id', 'desc')
            ->setOffset(0)
            ->setLimit(9999);
        $order_handler = new \Biz\OrderHandler();
        $order_handler->getOrderListByEnterpriseCustomerId($talabtion);
        $fliter_product_ids = array();
        if ($talabtion->getTotal() == 0 || $talabtion->getSuccessStatus() == false) { // 判断是否存在数据
            $result['product_list'] = array();
        } else {
            $order_list = $talabtion->getData();
            \Biz\OrderHandler::getOrderDetailByOrderList($order_list);
            foreach ($talabtion->getData() as $key => $order_cls) {
                foreach ($order_cls->getDetailList() as $k => $val) {
                    $fliter_product_ids[] = $val->getProductId();
                }
            }

            //去除重复producd_id
            $product_ids = array_unique($fliter_product_ids);
            //大于20则只取20条记录
            if(count($product_ids) > 20){
                $product_ids = array_slice($product_ids,0,20);
            }
            //循环通过产品id去读取数据
            $product_tmp_lists = array();
            $product_channel_handler = new \Biz\ProductHandler();
            foreach ($product_ids as $product_id){
                $tmp = array();
                $product_channel_lists = $product_channel_handler->getProductByChannelIdAndProductIds($channel_id, $product_id);
                foreach ($product_channel_lists as $product_channel_val){
                    $tmp = $product_channel_val;
                }
                $product_tmp_lists[] = $tmp;
            }
            $fliter_product_ids = array(); //  重新组装待处理的商品id
            foreach ($product_tmp_lists as $product) {
                $tmp_product_lists[$product['product_id']]['id'] = $product['product_id'];
                $tmp_product_lists[$product['product_id']]['enterprise_id'] = $product['enterprise_id'];
                $tmp_product_lists[$product['product_id']]['brand_id'] = $product['brand_id'];
                $tmp_product_lists[$product['product_id']]['brandname'] = (string)$product['brand_name'];
                $tmp_product_lists[$product['product_id']]['parent_brand_name'] = is_null($product['parent_brand_name']) ? '' : $product['parent_brand_name'];
                $tmp_product_lists[$product['product_id']]['parent_brand_id'] = is_null($product['parent_brand_id']) ? '' : $product['parent_brand_id'];;
                $tmp_product_lists[$product['product_id']]['name'] = (string)$product['name'];
                $tmp_product_lists[$product['product_id']]['icon_url'] = self::fullImgUrl($product['icon_url']);
                $tmp_product_lists[$product['product_id']]['mass_packing'] = (string)$product['sell_unit_string'];
                $tmp_product_lists[$product['product_id']]['remark'] = (string)$product['remark'];
                $tmp_product_lists[$product['product_id']]['channel_price'] = (string)$product['price'];
                $tmp_product_lists[$product['product_id']]['sort_price'] = (float)$product['price'];
                $fliter_product_ids[] = $product['product_id']; //重新组装待处理的商品id
            }

            $product_sku_list = $product_channel_handler->getOnsaleAttrByProductIdsAndChannelId($fliter_product_ids, $channel_id);// 得到sku信息
            \Biz\Market\Product::handelSkuListPriceByChannelIdAndEnterpriseId($product_sku_list, $channel_id, $enterprise_id); // 根据渠道id批量处理sku价格，与参与活动状态
            $result_product_list = array();

            foreach ($tmp_product_lists as $product_id => $item) {
                $result_product_list[$product_id] = $item;

                $result_product_list[$product_id]['sku_info'] = isset($product_sku_list[$product_id]) ? $product_sku_list[$product_id] : array();

                if (isset($product_sku_list[$product_id][0]['join_market_type'])) {
                    $result_product_list[$product_id]['activity_info'] = array(array('id' => 1)); // 只需标记参与活动即可
                } else {
                    $result_product_list[$product_id]['activity_info'] = array();
                }
            }
            $result['product_list'] = array_values($result_product_list);
        }
    }
    /**
     * 补全图片信息
     * @param $icon_url
     * @return string
     */
    protected static function fullImgUrl($icon_url)
    {
        $image_domain = 'http://image.freshqiao.com';
        $image_url = $icon_url == '/' ? $image_domain . $icon_url : $image_domain . '/' . $icon_url;
        return $image_url;
    }

}